import React, {Component} from 'react';
import styles from './index.module.css'
import collect from '../../../../assets/收藏.svg'
import collected from '../../../../assets/已收藏.svg'
import {Toast} from "antd-mobile";
import {del, require, require2} from "../../../../netWork";
import {getToken} from "../../../../utiles/token";
class BottomBar extends Component {
    state = {
        isCollect:false
    };
    componentDidMount() {
        this.getCollect()
    }

    //判断是否收藏,已改变收藏图片
    getCollect = () =>{
        const isLogin = getToken();
        if(isLogin){
            const {value} = this.props;
            require({
                url:`/user/favorites/${value}`,
                headers:{authorization:getToken()}
            }).then(res =>{
                if(res.status === 200){
                    this.setState({
                        isCollect:res.body.isFavorite
                    })
                }
            })
        }else {
            return null
        }
    };

    change = () =>{
        const islogin = getToken();
        if(islogin){
            this.collected();
        }else {
            const {route} = this.props;
             route.history.push('/login')
        }
    };
    //要收藏
    collected = () =>{
        const {isCollect} = this.state;
        const {value} = this.props;
        if(!isCollect){ //收藏
            require2({
                url:`/user/favorites/${value}`,
                headers:{authorization:getToken()}
            }).then(res =>{
                // console.log(res);
                if(res.status === 200){
                    this.setState({
                        isCollect: true
                    })
                    Toast.info('收藏成功!!!', 1);
                }
            })
        }else {  //请求取消收藏
            del({
                url:`/user/favorites/${value}`,
                headers:{authorization:getToken()}
            }).then(res =>{
                // console.log(res);
                if(res.status === 200){
                    this.setState({
                        isCollect: false
                    })
                    Toast.info('取消成功 !!!', 1);
                }
            })
        }

    }


    render() {
        const {isCollect} = this.state;
        return (
            <div className={styles.root}>
                <div className={styles.collect} onClick={this.change}>
                    {isCollect ? <img src={collected} className={styles.img} alt='收藏'/> :
                        <img src={collect} className={styles.img} alt="收藏"/>}
                    <span>收藏</span>
                </div>
                <div className={styles.consult}>
                    在线咨询
                </div>
                <div className={styles.call}>
                    电话预约
                </div>
            </div>
        );
    }
}

export default BottomBar;
